In Java, classes and objects form the foundation of object-oriented programming (OOP). A class is a blueprint that describes how objects of that type behave and what data they store. An object is a real instance created from that blueprint. You can create many different objects from the same class, and each object stores its own unique data.
A class defines two major parts:
These are the pieces of data every object of the class will store.
private String name;
private int age;
These are the actions the object can perform.
public void setAge(int a) { age = a; }
public int getAge() { return age; }
A class itself is NOT an object. It is only the design.
An object is created from a class using the new keyword.
Dog d1 = new Dog();
Dog d2 = new Dog();
Each of these statements creates a new Dog object in memory.
The variable (e.g., d1) is a reference variable that points to the object.
Even though objects come from the same class, they can hold different data.
Student s1 = new Student("Aiden", 95);
Student s2 = new Student("Jolie", 87);
s1 stores Aiden's data, while s2 stores Jolie's data. This is why objects represent individual items with their own state.
You access an object's behavior using the dot (.) operator:
s1.getGrade();
s2.setGrade(100);
Object variables do not store the object directly—they store a reference (memory address) pointing to the object.
Student s1 = new Student("Ben", 90);
Class: Car Blueprint
Objects: Actual Cars (Toyota, Honda, Tesla)
Each car shares the same design but has different colors, mileage, and owners.
This is exactly how class instances differ in Java.
| Term | Definition |
|---|---|
| Class | Blueprint defining attributes (state) and methods (behavior) |
| Object | A real instance created from a class |
| Instance Variable | Data stored inside each object |
| Method | Behavior an object can perform |
| Instantiation | Creating an object using new |
| State | The values stored in an object |
| Behavior | Actions the object performs |
| Reference Variable | Stores the memory address of an object |
public class Dog {
private String name;
private int age;
public Dog(String n, int a) {
name = n;
age = a;
}
public void bark() {
System.out.println(name + " says Woof!");
}
}
Dog d1 = new Dog("Max", 4);
Dog d2 = new Dog("Bella", 2);
d1.bark(); // Max says Woof!
d2.bark(); // Bella says Woof!
Understanding objects helps students succeed in:
this keywordGiven a class Book and a few reference declarations, determine:
Write a Player class with name and score, then create two different players in the client to show how they store different values.
Trace what happens to the values of two Counter objects when methods are called and references are reassigned.
Create a Rectangle class with area(), perimeter(), and isSquare().
Make three Rectangle objects and print their results.
Write a BankAccount class with deposit/withdraw.
Create two accounts in the client and show how their balances change independently.
Create an array of 4 Player objects, initialize them in a loop, and print each player's score.
Create a Pet class. Store several Pet objects in an ArrayList, then loop and call speak() on each.
toString()Add a meaningful toString() method to any class you wrote and print the object.
Practice scenarios where two references point to the same object, then one reference changes.
Create a class of your choice with at least 3 instance variables and 3 methods. Then create two distinct objects to show different stored values.